package com.jbit.hxy.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbit.hxy.common.Result;
import com.jbit.hxy.model.dto.AdminFromDTO;
import com.jbit.hxy.model.dto.AdminQueryDTO;
import com.jbit.hxy.model.po.Admin;
import com.jbit.hxy.model.vo.PageVO;
import com.jbit.hxy.service.user.AdminService;
import com.jbit.hxy.mapper.AdminMapper;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminServiceImpl  extends ServiceImpl<AdminMapper, Admin>
        implements AdminService {
    private final AdminMapper adminMapper;
    private final SqlSessionFactory sqlSessionFactory;
    @Override
    public Result getAdminList(AdminQueryDTO adminQueryDTO) {
        //使用Lambda表达式【指定需要查询的字段】
        IPage<Admin> page = new Page<>(adminQueryDTO.getCurPage(),
                adminQueryDTO.getPageSize());
        //2.设置查询条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        //管理员名称
        queryWrapper.lambda().like(StringUtils.isNotBlank(
                        adminQueryDTO.getName()),
                Admin::getName,
                adminQueryDTO.getName());
        //管理员手机号

        queryWrapper.lambda().like(StringUtils.isNotBlank(
                        adminQueryDTO.getPhone()),
                Admin::getPhone,
                adminQueryDTO.getPhone());
        //管理员类型

        queryWrapper.lambda().like(StringUtils.isNotBlank(
                        adminQueryDTO.getUserType()),
                Admin::getUserType,
                adminQueryDTO.getUserType());



        //查询指定字段
        //queryWrapper.select("id", "title");
        //排除字段查询
        queryWrapper.select(Admin.class, info -> !info.getColumn().equals("deleted"));
        //3.查询
        IPage<Admin> pageResult = adminMapper.selectPage(page, queryWrapper);

        //4.封装，返回
        PageVO pageVo = new PageVO<>();
        List<Admin> records = pageResult.getRecords();

//        records.forEach(admins -> {
//            if (admins.getIsDeleted() == 1) {
//                records.remove(admins);
//            }
//        });
//        pageResult.setRecords(records);
        pageVo.setTotal(pageResult.getRecords().size());
        pageVo.setList(records);

        pageVo.setPageSize(pageResult.getSize());
        pageVo.setCurrentPage(pageResult.getCurrent());


        return Result.success(pageVo);
    }

    @Override
    public Result addAdmin(AdminFromDTO adminFromDTO) {

        Admin admin = new Admin();
        BeanUtils.copyProperties(adminFromDTO,admin);
        admin.setCreateTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        if (adminMapper.selectCountByUserName(adminFromDTO.getUserName()) > 0) {
            return Result.fail("用户名已存在");
        }
        adminMapper.insert(admin);
        return Result.success("添加成功");
    }

    @Override
    public Result updateAdmin(AdminFromDTO adminFromDTO) {

        Admin admin = new Admin();
        BeanUtils.copyProperties(adminFromDTO,admin);
        admin.setUpdateTime(LocalDateTime.now());
        if (adminMapper.selectCountByUserName(adminFromDTO.getUserName()) > 1) {
            return Result.fail("用户名已存在");
        }
        adminMapper.updateById(admin);

        return Result.success("修改成功");
    }

    @Override
    public Result deleteAdmin(Long id) {

        adminMapper.deleteById(id);
        return Result.success("删除成功");
    }
}
